package com.example.wasaimovie.Controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wasaimovie.Annotation.DisableBaseResponse;
import com.example.wasaimovie.Model.entity.Activity;
import com.example.wasaimovie.Model.entity.Registration;
import com.example.wasaimovie.Model.support.ResponseResult;
import com.example.wasaimovie.Service.ActivityService;
import com.example.wasaimovie.Service.RegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api")
@Slf4j
public class ActivityController {

    @Autowired
    ActivityService activityService;

    @Autowired
    RegistrationService registrationService;

    /**
     * 查询活动列表
     * @return 活动信息
     */
    @GetMapping("/activity")
    @CrossOrigin
    public List<Activity> getActivity(){
        log.info("ActivityController -> getActivity");
        List<Activity> list = activityService.list();
        return list;
    }

    /**
     * 报名活动
     * @param registration 活动信息和用户信息
     * @return 报名是否成功
     */
    @CrossOrigin
    @PostMapping("/registration")
    @DisableBaseResponse
    public ResponseResult<Object> signUpActivity(@RequestBody Registration registration){
        log.info("ActivityController -> signUpActivity");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        // 查询是否已经参加活动
        QueryWrapper<Registration> qw = new QueryWrapper<>();
        qw.eq("aid", registration.getAid());
        qw.eq("uid", registration.getUid());
        if (!registrationService.list(qw).isEmpty()){
            // 如果查询结果不为空
            System.out.println("已经报名过该活动");
            return new ResponseResult<>(400, "您已经报名过该活动");
        }
        registration.setId(UUID.randomUUID().toString());
        registration.setCreateTime(format.format(new Date()));

        registrationService.save(registration);
        return new ResponseResult<>(200);
    }

}
